package com.example.final_project.Usr.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.ejlchina.okhttps.OkHttps;
import com.example.final_project.MainView;
import com.example.final_project.R;
import com.example.final_project.ShoppingCar.Activity.ShoppingBuyActivity;
import com.example.final_project.ShoppingCar.Activity.ShoppingCarActivity;
import com.example.final_project.ShoppingCar.frag.ShoppingCarFragment;
import com.example.final_project.Usr.adapter.ShopInAdapter;
import com.example.final_project.Usr.entry.UserStore;

import java.util.List;

public class ShopActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private Button add,buy;
    private ImageView car,back;
    private Context context;
    private int Id;
    private UserStore userStore;
    public static final int STORE = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);

        //接受传入的数据
        Intent intent = this.getIntent();
        Id = intent.getIntExtra("id",0);

        findById();
        clickManage();
        getStoreData();

    }

    Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case STORE:
                    List<UserStore> list = (List<UserStore>) msg.obj;
                    userStore = list.get(0);
                    ShopInAdapter shopInAdapter = new ShopInAdapter(userStore,context);
                    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false);
                    recyclerView.setAdapter(shopInAdapter);
                    recyclerView.setLayoutManager(linearLayoutManager);
                    break;
            }

        }
    };

    //获取网络请求
    private void getStoreData(){
        OkHttps.async("http://10.150.11.9:8086/chengcar/storeid")
                .bind(this)
                .addUrlPara("id",Id)
                .setOnResList(UserStore.class,store ->{
                    Message message = Message.obtain();
                    message.what = STORE;
                    message.obj = store;
                    handler.sendMessage(message);
                })
                .get();
    }

    //初始化控件
    private void findById(){
        context = this.getBaseContext();
        recyclerView = this.findViewById(R.id.home_shop_in);
        add = this.findViewById(R.id.home_shop_in_add);
        buy = this.findViewById(R.id.home_shop_in_buy);
        car = this.findViewById(R.id.home_shop_in_car);
        back = this.findViewById(R.id.home_shop_in_back);
    }
    //点击事件
    private void clickManage(){
        //前往购物车
        car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, MainView.class);
                intent.setAction("backToCar");
                context.startActivity(intent);
            }
        });

        //添加购物车
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,MainView.class);
                intent.setAction("backToCar");
                intent.putExtra("id",Id);
                startActivity(intent);
            }
        });

        //购买
        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ShoppingBuyActivity.class);
                intent.putExtra("id",userStore.getId());
                context.startActivity(intent);
            }
        });

        //返回个人中心
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ShopActivity.this, MainView.class);
                intent.setAction("backToHome");
                startActivity(intent);
            }
        });
    }

}